package com.gamer.p1.util;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author 种鑫
 * @date 2024/4/26 9:24
 */
@Slf4j
public class AESUtil {
    public static final String KEY = "6SfchHvVB43Y20WywilaZ8TbEjCpJRdL";
    private static final String SECRET_KEY = "6SfchHvVB43Y20WywilaZ8TbEjCpJRdL";

    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }

    /**
     * AES加密
     *
     * @param originalString
     * @return
     * @throws Exception
     */
    public static String encrypt(String originalString) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] encryptedBytes = cipher.doFinal(originalString.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            log.error("", e);
        }
        return "";
    }

    /**
     * AES解密
     *
     * @param encryptedString
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptedString) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedString));
            return new String(decryptedBytes);
        } catch (Exception e) {
            log.error("", e);
        }
        return "";
    }


    public static void main(String[] args) throws Exception {
//        try {
//            String originalString = "{\n" +
//                    "    \"Name\": \"SaveToServerFlag\",\n" +
//                    "    \"CompatibleVer\": -1,\n" +
//                    "    \"IsSaving\": 0,\n" +
//                    "    \"LastSaveTime\": 0,\n" +
//                    "    \"AppVersion\": \"3.2.3.1\",\n" +
//                    "    \"Ver\": 0\n" +
//                    "}";
//            String encryptedString = encrypt(originalString);
//            System.out.println("Encrypted: " + encryptedString);
////            String encryptedString = "rwZb1VsE0Ku6tqfgk/4/0g==";
//            String decryptedString = decrypt(encryptedString);
//            System.out.println("Decrypted: " + decryptedString);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        String s = "w81HLb0ZiAMHoUPei9nofkXj6UziaQMTC75A6sdlOr0=";
        String encryptedString = decrypt(s);
        System.out.println("Decrypted: " + encryptedString.getBytes());

    }
}
